import { PAY_SUCCESS_URL, PAY_FAIL_URL, KEY_ORDER_DETAILS } from "./constants";

export function setOrderCache(paySuccessUrl, payFailUrl, orderResult) {
  sessionStorage.setItem(PAY_SUCCESS_URL, paySuccessUrl);
  sessionStorage.setItem(PAY_FAIL_URL, payFailUrl);
  if (typeof orderResult === "object") {
    sessionStorage.setItem(KEY_ORDER_DETAILS, JSON.stringify(orderResult));
  }
}

export function getOrderCache() {
  const orderJSON = sessionStorage.getItem(KEY_ORDER_DETAILS);
  const order = orderJSON ? JSON.parse(orderJSON) : null;
  return {
    order,
    paySuccessUrl: sessionStorage.getItem(PAY_SUCCESS_URL),
    payFailUrl: sessionStorage.getItem(PAY_FAIL_URL),
  };
}

export function clearOrderCache() {
  sessionStorage.removeItem(KEY_ORDER_DETAILS);
  sessionStorage.removeItem(PAY_SUCCESS_URL);
  sessionStorage.removeItem(PAY_FAIL_URL);
}
